﻿using System;
using IAM.Interface.Common.Context;
using IAM.Interface.Common.Abnormity;
using System.Configuration;
using ResponseLocalLibrary;

namespace BEGDC.CC.Web
{
    /// <summary>
    /// Summary description for Response.
    /// </summary>
    public partial class Response : System.Web.UI.Page
    {
        private void Page_Load(object sender, System.EventArgs e)
        {
            // Put user code to initialize the page here
            if(!IsPostBack)
                TransactData();
        }
        #region 数据处理
        /// <summary>
        /// 数据处理
        /// 0.实例化响应端发送消息上下文控制对象
        /// 0.1判断请求ip是否安全
        /// 1.接收xml请求
        /// 1.1加密类型
        /// 1.2密钥
        /// 1.3xml信息
        /// 2.实例化ResponseContextModel
        /// 4.响应端处理，返回xml包
        /// 5.回写数据流
        /// </summary>
        private void TransactData()
        {
            try
            {
                //实例化响应端发送消息上下文控制对象
                ResponseContextControl rcc = new ResponseContextControl();
                //0.1判断请求ip是否安全
                bool validate = rcc.ValidateRequestIP(ConfigurationManager.AppSettings["RequestIPs"]);
                if (!validate)
                {
                    ResponseException rex = new ResponseException();
                    rex.StatusCode = 418;
                    throw (rex);      
                }
                //1.接收xml请求
                //1.3xml信息
                string xmlinfo = Request.Form["xml"];
                string key = ResponseContextControl.GetConfigKey();
                //1.4 签名 
                string sign = "";

                sign = Request.Form["sign"];
                //2.实例化ResponseContextModel
                ResponseContextModel rcm = new ResponseContextModel(
                    sign,
                    0,
                    key,
                    xmlinfo);
                //3.实例化响应端发送消息上下文控制对象
                rcc.ContextModel = rcm;


                //4.响应端处理，返回xml包
                string strresult = rcc.ContextControl();
                //5.回写数据流
                Response.Write(strresult);
            }
            catch (ResponseException re)
            {
                throw (re);
            }
            catch (Exception ex)
            {
                throw (ex);
            }
        }
        #endregion

        #region Web Form Designer generated code
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: This call is required by the ASP.NET Web Form Designer.
            //
            InitializeComponent();
            base.OnInit(e);
        }

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.Load += new System.EventHandler(this.Page_Load);
        }
        #endregion
    }
}
